package reduce.fz.com.reduce.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.SubjectReply;
import reduce.fz.com.reduce.bean.TeamSubject;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.delegate.RecyclerViewDelegate;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.fragment.base.BaseListHeaderFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class SubjectDetailFragment extends BaseListHeaderFragment<BaseRecycleViewHolder, SubjectReply,TeamSubject> {

    private Long teamid;
    private TeamSubject teamsubject;

    private RecyclerViewDelegate mRecyclerViewDelegate = new RecyclerViewDelegate();


    public boolean isViewBeingDragged(MotionEvent event) {
        return mRecyclerViewDelegate.isViewBeingDragged(event, recyclerView);
    }


    public static SubjectDetailFragment create(Long teamid,TeamSubject subject) {
        SubjectDetailFragment fragment = new SubjectDetailFragment();
        fragment.setSupportLoadingDialog(false);
        Bundle bundle = new Bundle();
        bundle.putLong("teamid", teamid);
        bundle.putSerializable("subject",subject);
        fragment.setArguments(bundle);
        return fragment;
    }

    public void addReply(SubjectReply reply)

    {
        addLast(reply);
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        teamid = getArguments().getLong("teamid");
        teamsubject= (TeamSubject) getArguments().getSerializable("subject");

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view= super.onCreateView(inflater, container, savedInstanceState);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        addHeader(teamsubject);
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        //默认返回列表
        return null;
    }

    @Override
    protected BaseRecycleViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.team_subject_reply_item, parent, false);
        SubjectReplyHolder holder = new SubjectReplyHolder(view);
        return holder;
    }

    @Override
    protected BaseRecycleViewHolder createHeaderViewHolder(ViewGroup parent) {
        View view=layoutInflater.inflate(R.layout.view_subject_header,parent,false);
         SubjectHeaderHolder holder=new SubjectHeaderHolder(view);
        return holder;
    }

    @Override
    protected BaseRecycleViewHolder createfootViewHolder(View view) {
        return new SubjectReplyHolder(view);
    }

    //请求URL
    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<SubjectReply>>(new TypeReference<Result<List<SubjectReply>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
            }

            @Override
            public void responseInfo(Result<List<SubjectReply>> data) {

                addResult( data);
            }
        });
    }

    @Override
    protected String setUrlBeforeRequest(List<SubjectReply> datas, Pagination pagination) {
        String url = ApiUtil.getApi(context, R.array.api_query_subject_reply, teamsubject.subjectid, pagination.getPageNo(), pagination.getPageSize());
        return url;
    }

    @Override
    protected int getPageType() {
        return Pagination.TYPE_START_NUM;
    }


    public static class SubjectHeaderHolder  extends BaseRecycleViewHolder<Object >
    {
        private TextView tv_subject;

        public SubjectHeaderHolder(View itemView) {
            super(itemView);
            tv_subject= (TextView) itemView.findViewById(R.id.tv_subject);
        }


        @Override
        public void renderView(Object data, int position) {
            TeamSubject teamSubject=(TeamSubject)data;
            tv_subject.setText(teamSubject.description);

        }
    }



    public static class SubjectReplyHolder extends BaseRecycleViewHolder<SubjectReply> {
        private RoundNetworkImageView mUserHeader;
        private TextView mTvNickName;
        private TextView pubTime;
        private TextView mDesc;

        public SubjectReplyHolder(View itemView) {
            super(itemView);
            mUserHeader = (RoundNetworkImageView) itemView.findViewById(R.id.rnv_user_header);
            mTvNickName = (TextView) itemView.findViewById(R.id.tv_user_nickname);
            pubTime = (TextView) itemView.findViewById(R.id.tv_reply_time);
            mDesc = (TextView) itemView.findViewById(R.id.tv_reply_content);
        }

        @Override
        public void renderView(SubjectReply data, int position) {
            SubjectReply reply=data;
            if (reply.user == null) return;
            mUserHeader.setImageUrl(reply.user.avatar, HttpUtils.getAvatarImageLoader());
            mTvNickName.setText(reply.user.nickname);
            mDesc.setText(reply.description);
            pubTime.setText(DateUtil.getInterval(reply.pubtime));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }
}
